<?php

/*
 * 	Codificado: 02-Noviembre-2011, 00:31
	Estado: Ajustado_Modelo_Actual(X) Funcionando()
 */


class Asignaturas_Model extends CI_Model{

	private $table = 'Asignaturas';

	function _required($required, $data)
	{
		foreach($required as $field)
		if(!isset($data[$field])) return false;

		return true;
	}

	function Insertar($options = array()) {

		/*if ($this->_required(array('IdDocente','Codigo','Ano','Nombre','Programa','Facultad','IdiomaExtranjero',
							'Proporcion'), $options))
		return FALSE ;*/

		$this->db->insert($this->table, $options);
		return $this->db->insert_id();

	}

	function Editar($options = array()) {

		if(!$this->_required(array('Id'),$options))
		return FALSE;

		unset($options['submit'] );

		if(isset($options['IdDocente']))
		$this->db->set('IdDocente', $options['IdDocente']);

		if(isset($options['Codigo']))
		$this->db->set('Codigo', $options['Codigo']);

		if(isset($options['Ano']))
		$this->db->set('Ano', $options['Ano']);
			
		if(isset($options['Codigo']))
		$this->db->set('Codigo', $options['Codigo']);

		if(isset($options['Nombre']))
		$this->db->set('Nombre', $options['Nombre']);

		if(isset($options['Programa']))
		$this->db->set('Programa', $options['Programa']);

		if(isset($options['Facultad']))
		$this->db->set('Codigo', $options['Codigo']);

		if(isset($options['PlanEstudios']))
		$this->db->set('PlanEstudios', $options['PlanEstudios']);
		
		if(isset($options['IdiomaExtranjero']))
		$this->db->set('IdiomaExtranjero', $options['IdiomaExtranjero']);

		if(isset($options['Proporcion']))
		$this->db->set('Proporcion', $options['Proporcion']);
			
		$this->db->where('Id', $options['Id']);
		$this->db->update($this->table, $options);

		return $this->db->affected_rows();
	}

	function Eliminar($options = array()) {

		$this->db->where('Id', $options['Id']);
		$this->db->delete($this->table);

		if($data['error'] = $this->db->_error_message());
		return $data;

	}

	function Ver($options = array()) {

		$this->db->where('Id', $options['Id']);
		$query = $this->db->get($this->table);

		if(isset($options['Id']))
		return $query->row_array(0);

		return $query->result_array();
	}

	function Listar($options = array()) {

		$this->db->select('Id,Ano,Nombre,Programa,IdiomaExtranjero' );

		if(isset($options['limit']) && isset($options['offset']))
		$this->db->limit($options['limit'], $options['offset']);
		else if(isset($options['limit']))
		$this->db->limit($options['limit']);
			
		$query = $this->db->get($this->table);
		if(isset($options['count']))
		return $query->num_rows();
			
		return $query->result_array();
	}


	function BuscarPorAno($options = array()) {
		if ($this->_required(array('Ano'), $options)) return FALSE ;

		$this->db->where('Ano', $options['Ano']);
		$query = $this->db->get($this->table);

		return $query->result_array();
	}

	function BuscarPorFacultad($options = array()) {

		if ($this->_required(array('Facultad'), $options)) return FALSE ;

		$this->db->where('Facultad', $options['Facultad']);
		$query = $this->db->get($this->table);

		return $query->result_array();
	}

	function BuscarPorPrograma($options = array()) {
		if ($this->_required(array('Programa'), $options)) return FALSE ;

		$this->db->where('Programa', $options['Programa']);
		$query = $this->db->get($this->table);

		return $query->result_array();
	}
	function BuscarPorDocente($options = array()) {
		if ($this->_required(array('IdDocente'), $options)) return FALSE ;

		$this->db->where('IdDocente', $options['IdDocente']);
		$query = $this->db->get($this->table);

		return $query->result_array();
	}
}
